package org.supda.system.controller.system;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import org.supda.system.annoation.SysLog;
import org.supda.system.controller.common.BaseController;
import org.supda.system.entity.TRole;
import org.supda.system.entity.TUser;
import org.supda.system.exception.ParameterException;
import org.supda.system.service.RoleService;
import org.supda.system.service.UserService;
import org.supda.system.util.DataTablePageUtil;
import org.supda.system.util.EntityUpdataUtils;

/**
 * 
 * @author supda
 * @Date 2016-12-29 下午2:22:16
 * 用户管理
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController{
	
	@Inject
	private UserService userService;
	
	@Inject RoleService roleService;
	
	/**
	 * 加载用户管理页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"/",""}, method=RequestMethod.GET, produces="text/html;charset=utf-8")
	public String userUI(HttpServletRequest request, Model model){
		//查询出子菜单的所有操作
		model.addAttribute("menuId",getOID());
		return SYSTEM_MANAGEMENT_VIEW_PACKAGE+"user/user";
	}
	
	/**
	 * 查询用户
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/search", method=RequestMethod.POST, produces="text/html;charset=utf-8")
    public @ResponseBody DataTablePageUtil<TUser> search(WebRequest request){  
    	DataTablePageUtil<TUser> dtp = new DataTablePageUtil<TUser>(request); 
    	//开启Or查询
    	userService.listByDataTable(dtp);
    	return dtp; 
    }
	
	/**
	 * 跳转到详情页面
	 * @return
	 */
	@SysLog(module="用户管理", method="查看", desc="用户信息查看")
	@RequestMapping(value = "/show",method=RequestMethod.GET , produces = "text/html; charset=utf-8")
	public String show(
			HttpServletRequest request,
			@RequestParam(value="id") Long id, 
			Model model) {
		
		if(id==null || id==0){
			throw new ParameterException("用户管理-参数异常:"+id);
		}
		
		TUser user = userService.findById(id);
		model.addAttribute("user" , user);
		
		return SYSTEM_MANAGEMENT_VIEW_PACKAGE+"user/user-show";
	}
	
	/**
	 * 跳转到编辑页面
	 * @param request
	 * @param userId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit" , method = RequestMethod.GET ,  produces = "text/html; charset=utf-8")
	public String editUI(HttpServletRequest request,
			@RequestParam(value="id") Long id, 
			Model model){
		
		TUser user = userService.findById(id);
		
		List<TRole> tRoles = roleService.findAllRole();
		
		model.addAttribute("user" , user);
		model.addAttribute("roles",tRoles);
		
		return SYSTEM_MANAGEMENT_VIEW_PACKAGE+"user/user-edit";
	}
	
	/**
	 * 用户编辑
	 * @param request
	 * @param userId
	 * @param model
	 * @return
	 */
	@SysLog(module="用户编辑", method="edit", desc="用户编辑-编辑")
	@RequestMapping(value = "/edit" , method = RequestMethod.POST ,  produces = "text/html; charset=utf-8")
	public @ResponseBody String edit(HttpServletRequest request,
			Model model,
			TUser user,
			Long[] roleId){		
		try{
			userService.update(user , roleId);
			return RESPONSE_SUCCESS;
		}catch(Exception e){
			return RESPONSE_FAIL;
		}
		
	}
	
	/**
	 * 1.进入添加用户页面 GET:/user_create
	 */
	@RequestMapping(value = "/create" , method = RequestMethod.GET ,  produces = "text/html; charset=utf-8")
	public String createUI(HttpServletRequest request,
			Model model){
		
		return SYSTEM_MANAGEMENT_VIEW_PACKAGE+"user/user-create";
	}
	
	
	/**
	 * 2.添加用户 POST:/user_create
	 */
	@SysLog(module="用户新增", method="create", desc="用户新增")
	@RequestMapping(value = "/create" , method = RequestMethod.POST ,  produces = "text/html; charset=utf-8")
	public @ResponseBody String create(HttpServletRequest request,
			TUser user, 
			Model model){

		 user = userService.createUser(user);
	
		 if(user !=null)
			 return RESPONSE_SUCCESS;
		 else
			 return RESPONSE_FAIL;
	}
	
	/**
	 * 3.删除用户 DELETE:/user_delete?id={id}
	 */
	@SysLog(module="删除用户", method="create", desc="删除用户")
	@RequestMapping(value = "/delete" , method = RequestMethod.POST ,  produces = "text/html; charset=utf-8")
	public @ResponseBody String delete(HttpServletRequest request,
			@RequestParam(value="id") Long id, 
			Model model){
	
		String message = userService.delete(id);
		
		return message;
	}
	
	/**
	 * 5.锁定用户 PUT:/user_locked?id={id}
	 */
	@SysLog(module="锁定用户", method="create", desc="锁定用户")
	@RequestMapping(value = "/locked" , method = RequestMethod.POST ,  produces = "text/html; charset=utf-8")
	public @ResponseBody String locked(HttpServletRequest request,
			@RequestParam(value="id") Long id, 
			Model model){
		
		String message = userService.locked(id);

		return message;
	}
}
